/**
 * Copyright (C) 2012 Eduard Sedov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.es.eventbus.annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.es.eventbus.TopicListener;

/**
 *
 * @author ESEDO
 */
public class ProxyTopicListener implements TopicListener {

    private Object target;
    private Method method;

    public ProxyTopicListener(Object target, Method method) {
        this.target = target;
        this.method = method;
    }

    public void onEvent(String topic, Object event) {
        try {
            if (method.getParameterTypes().length == 2) {
                method.invoke(target, new Object[]{topic, event});
            } else {
                method.invoke(target, new Object[]{event});
            }
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }
}
